package com.zzyl.nursing.controller.member;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.memberDto.ReservationDto;
import com.zzyl.nursing.dto.memberDto.ReservationPageDto;
import com.zzyl.nursing.service.member.IMemberReservationService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/member/reservation")
public class MemberReservationController {

    private final IMemberReservationService reservationService;

    @GetMapping("/cancelled-count")
    public AjaxResult cancelledCount() {
        Integer count = reservationService.cancelledCount();
        return AjaxResult.success(count);
    }

    /**
     * 保存预约信息
     * @param reservationDto
     * @return
     */
    @PostMapping
    public AjaxResult save(@RequestBody ReservationDto reservationDto) {
        reservationService.saveReservation(reservationDto);
        return AjaxResult.success();
    }

    /**
     * 分页查询预约信息
     * @param reservationPageDto
     * @return
     */
    @GetMapping("/page")
    public AjaxResult page(ReservationPageDto reservationPageDto) {
        IPage<Reservation> result = reservationService.pageReservation(reservationPageDto);
        TableDataInfo<Reservation> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(result.getTotal());
        tableDataInfo.setRows(result.getRecords());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return AjaxResult.success(tableDataInfo);
    }

    /**
     * 取消预约
     *
     * @param id
     * @return
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult cancelReservation(@PathVariable("id") Long id) {
        reservationService.cancelReservation(id);
        return AjaxResult.success();
    }
}
